<template>
  <div>
    <Back :title="$t('MyInfo_userInfo')"></Back>
    <div class="from">
      <div class="title">
        {{ $t('MyInfo_updatePassword')}}
      </div>
      <div class="frombox">
        <h4>{{user}}</h4>
        <el-form ref="form" label-width="80px">
          <el-form-item :label=" $t('MyInfo_oldpassword')">
            <el-input type="password" v-model.trim="oldpassword" :placeholder=" $t('MyInfo_inputOldpassword')" @keyup.13.native="submit"></el-input>
          </el-form-item>
          <el-form-item :label=" $t('MyInfo_newpassword')">
            <el-input type="password" v-model.trim="newpassword1" :placeholder=" $t('MyInfo_updateImmediately')" @keyup.13.native="submit"></el-input>
          </el-form-item>
          <el-form-item :label=" $t('MyInfo_confirmPassword')">
            <el-input type="password" v-model.trim="newpassword2" :placeholder=" $t('MyInfo_inputNewpassword')" @keyup.13.native="submit"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button style="background: #067FB5; color: #fff;" @click="submit"> {{ $t('MyInfo_updateImmediately')}} </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import { PageHeader, Form, FormItem, Input, Button } from 'element-ui'
import Back from '@/components/Back'
import http from '@/utils/http'
Vue.use(PageHeader)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Input)
Vue.use(Button)

export default {
  data () {
    return {
      user: '',
      oldpassword: '',
      newpassword1: '',
      newpassword2: ''
    }
  },
  mounted () {
    // 获取localStorage存储的用户信息
    this.user = JSON.parse(localStorage.getItem('user')).username
  },
  methods: {
    // 提交修改密码
    submit () {
      let reg = /^[0-9A-Za-z]{6,16}$/
      let flag1 = reg.test(this.newpassword1)
      let flag2 = reg.test(this.newpassword2)
      if (!flag1 || !flag2) {
        return this.$message({
          showClose: true,
          message: '请输入6-16位的数字或字母！',
          type: 'error'
        })
      }
      if (this.newpassword1 === this.newpassword2) {
        http({
          url: '/password',
          method: 'POST',
          data: {
            oldpassword: this.oldpassword,
            newpassword: this.newpassword1
          }
        }).then(res => {
          if (res.Code === 200) {
            this.$message({
              showClose: true,
              message: '密码修改成功，请重新登录！',
              type: 'success'
            })
            localStorage.removeItem('user')
            localStorage.removeItem('userList')
            this.$router.push('/Login')
          } else {
            this.$message({
              showClose: true,
              message: '密码修改失败，请检查原密码是否输入错误！',
              type: 'error'
            })
          }
        }).catch(err => {
          this.$message({
            showClose: true,
            message: err.data,
            type: 'error'
          })
        })
        this.oldpassword = this.newpassword1 = this.newpassword2 = ''
      } else {
        this.$message({
          showClose: true,
          message: '两次密码输入不一致！',
          type: 'error'
        })
        this.newpassword1 = this.newpassword2 = ''
      }
    }
  },
  components: {
    Back
  }
}
</script>

<style lang="scss" scoped>
.from{
  margin-top: 30px;
}
.frombox{
  width: 500px;
  border: 1px solid #e9e7e7;
  padding: 20px;
  border-radius: 10px;
  margin: 20px auto;
  h4{
    text-align: center;
    line-height: 60px;
  }
}
</style>
